#include "Ext/FilterQuery.h"

#include "NewsTable.h"
#include "DatabaseException.h"

using namespace std;

namespace TCServer
{
    namespace Store
    {

        quint64 NewsTable::insert(const Type::News &news)
        {
            FilterQuery q;
            q.prepare(R"(

			INSERT INTO `tc`.`news`
			(`groupId`,
			`iconTitle`,
			`iconColor`,
			`title`,
			`author`,
			`contentUrl`)
			VALUES
			(?,
			?,
			?,
			?,
			?,
			?);

			)");
            q.addBindValue(news.groupId);
            q.addBindValue(news.iconTitle);
            q.addBindValue(news.iconColor);
            q.addBindValue(news.title);
            q.addBindValue(news.author);
            q.addBindValue(news.contentUrl);

            q.exec();
            return q.lastInsertId().toULongLong();
        }

        void NewsTable::update(const Type::News &news)
        {
            FilterQuery q;
            q.prepare(R"(

			UPDATE `tc`.`news`
			SET
			`groupId` = ?,
			`iconTitle` = ?,
			`iconColor` = ?,
			`title` = ?,
			`author` = ?,
			`contentUrl` = ?
			WHERE `id` = ?;

			)");
            q.addBindValue(news.groupId);
            q.addBindValue(news.iconTitle);
            q.addBindValue(news.iconColor);
            q.addBindValue(news.title);
            q.addBindValue(news.author);
            q.addBindValue(news.contentUrl);
            q.addBindValue(news.id);

            return q.exec();
        }

        void NewsTable::remove(quint64 id)
        {
            FilterQuery q;
            q.prepare(R"(

			DELETE FROM `tc`.`news`
			WHERE `id` = ?;

			)");
            q.addBindValue(id);

            q.exec();

            if (q.numRowsAffected() != 1)
                throw DatabaseException(q);
        }

        QList<TCServer::Type::News> NewsTable::queryAll(quint64 groupId)
        {
            FilterQuery q;
            q.prepare(R"(

			SELECT `news`.`id`,
				`news`.`time`,
				`news`.`groupId`,
				`news`.`iconTitle`,
				`news`.`iconColor`,
				`news`.`title`,
				`news`.`author`,
				`news`.`contentUrl`
			FROM `tc`.`news`
			WHERE `groupId` = ?;

			)");
            q.addBindValue(groupId);

            QList<Type::News> res;
            q.exec();

            while (q.next())
            {
                res.append(TCServer::Type::News
                {
                    q.nextValue().toULongLong(),
                    q.nextValue().toDateTime(),
                    q.nextValue().toULongLong(),
                    q.nextValue().toString(),
                    q.nextValue().toString(),
                    q.nextValue().toString(),
                    q.nextValue().toString(),
                    q.nextValue().toString()
                });
            }
            return res;
        }

        TCServer::Type::News NewsTable::query(quint64 id)
        {
            FilterQuery q;
            q.prepare(R"(

			SELECT `news`.`id`,
				`news`.`time`,
				`news`.`groupId`,
				`news`.`iconTitle`,
				`news`.`iconColor`,
				`news`.`title`,
				`news`.`author`,
				`news`.`contentUrl`
			FROM `tc`.`news`
			WHERE `id` = ?;

			)");
            q.addBindValue(id);

            QList<Type::News> res;
            q.exec();
            q.next();
            return TCServer::Type::News
            {
                q.nextValue().toULongLong(),
                q.nextValue().toDateTime(),
                q.nextValue().toULongLong(),
                q.nextValue().toString(),
                q.nextValue().toString(),
                q.nextValue().toString(),
                q.nextValue().toString(),
                q.nextValue().toString()
            };
        }

    }
}
